package com.qf.wangqing.sportsnews.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.qf.wangqing.sportsnews.BR;
import com.qf.wangqing.sportsnews.MyApplication;
import com.qf.wangqing.sportsnews.R;
import com.qf.wangqing.sportsnews.activity.MainActivity;
import com.qf.wangqing.sportsnews.adapter.CommentAdapter;
import com.qf.wangqing.sportsnews.bean.FragmentCornpetitionHot;
import com.qf.wangqing.sportsnews.bean.FragmentCornpetitionHotContest;
import com.qf.wangqing.sportsnews.databinding.ActivityMainBinding;
import com.qf.wangqing.sportsnews.databinding.FragmentCompetitionBinding;
import com.qf.wangqing.sportsnews.utils.CompetitionPoWinShare;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class CompetitionFragment extends Fragment implements Callback<FragmentCornpetitionHot>, View.OnClickListener {
    private FragmentCompetitionBinding binding;
    private CompetitionPoWinShare poWinShare;
    private TextView textView;
    private ActivityMainBinding mainBinding;


    private  CommentAdapter adapter;
  //  private CommentRecyclerAdapter adapter;


    public CompetitionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        binding= DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_competition, container, false);
        binding.fragmentCompetitionText.setOnClickListener(this);

      adapter=new CommentAdapter<>(getActivity(), new ArrayList<FragmentCornpetitionHotContest>(),R.layout.fragment_competition_recycle_item, BR.list );

       binding.mainListview.setAdapter(adapter);
        return binding.getRoot();


    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MyApplication application= (MyApplication) getActivity().getApplication();
        application.getCompetitionService().getList().enqueue(this);

        //
        binding.imageHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity activity = (MainActivity) getActivity();
                mainBinding = activity.getBinding();
                if (mainBinding.slidingMain.isOpen()) {
                    mainBinding.slidingMain.closePane();
                }else {
                    mainBinding.slidingMain.openPane();
                }
            }
        });
    }

    @Override
    public void onResponse(Call<FragmentCornpetitionHot> call, Response<FragmentCornpetitionHot> response) {
        FragmentCornpetitionHot body=response.body();

        adapter.addAll(body.getData().getContest_list());

        binding.setName(body.getData());

    }

    @Override
    public void onFailure(Call<FragmentCornpetitionHot> call, Throwable t) {

    }

    @Override
    public void onClick(View view) {
         ImageView imageView= (ImageView) view.findViewById(R.id.fragment_competition_image);
         RotateAnimation animation=new RotateAnimation(0,180,imageView.getWidth()/2,imageView.getHeight()/2);
         animation.setDuration(1000);
         imageView.setAnimation(animation);

         OnClickLintener onClickLintener=new OnClickLintener();

        poWinShare=new CompetitionPoWinShare(getContext(),onClickLintener, 400,800);
        poWinShare.showAsDropDown(view);
        poWinShare.getContentView().setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    poWinShare.dismiss();
                }
            }
        });
        poWinShare.setFocusable(true);
        poWinShare.showAsDropDown(view,0,0);
        poWinShare.update();


    }
    class OnClickLintener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.layout_share:
                    adapter=new CommentAdapter<>(getActivity(), new ArrayList<FragmentCornpetitionHotContest>(),R.layout.fragment_competition_recycle_item, BR.list );

                    binding.mainListview.setAdapter(adapter);
                   ((TextView) binding.fragmentCompetitionText.getChildAt(0)).setText("热门比赛");
                    MyApplication application= (MyApplication) getActivity().getApplication();
                 application.getCompetitionService().getList().enqueue(new Callback<FragmentCornpetitionHot>() {
                     @Override
                     public void onResponse(Call<FragmentCornpetitionHot> call, Response<FragmentCornpetitionHot> response) {
                         FragmentCornpetitionHot body=response.body();

                         adapter.addAll(body.getData().getContest_list());

                         binding.setName(body.getData());
                     }

                     @Override
                     public void onFailure(Call<FragmentCornpetitionHot> call, Throwable t) {

                     }
                 });


                    poWinShare.dismiss();
                    break;
                case R.id. popwin_all:
                    adapter=new CommentAdapter<>(getActivity(), new ArrayList<FragmentCornpetitionHotContest>(),R.layout.fragment_competition_recycle_item, BR.list );

                    binding.mainListview.setAdapter(adapter);
                    ((TextView) binding.fragmentCompetitionText.getChildAt(0)).setText("全部");
                    MyApplication applicationALL= (MyApplication) getActivity().getApplication();
                    applicationALL.getCompetitionService().getData().enqueue(new Callback<FragmentCornpetitionHot>() {
                        @Override
                        public void onResponse(Call<FragmentCornpetitionHot> call, Response<FragmentCornpetitionHot> response) {
                            FragmentCornpetitionHot body=response.body();

                            adapter.addAll(body.getData().getContest_list());

                            binding.setName(body.getData());

                        }

                        @Override
                        public void onFailure(Call<FragmentCornpetitionHot> call, Throwable t) {

                        }
                    });
                    poWinShare.dismiss();
                    break;
                case R.id. popwin_NBA:
                    adapter=new CommentAdapter<>(getActivity(), new ArrayList<FragmentCornpetitionHotContest>(),R.layout.fragment_competition_recycle_item, BR.list );

                    binding.mainListview.setAdapter(adapter);
                    ((TextView) binding.fragmentCompetitionText.getChildAt(0)).setText("NBA");
                    MyApplication applicationNBA= (MyApplication) getActivity().getApplication();
                    applicationNBA.getCompetitionService().getNBA().enqueue(new Callback<FragmentCornpetitionHot>() {
                        @Override
                        public void onResponse(Call<FragmentCornpetitionHot> call, Response<FragmentCornpetitionHot> response) {
                            FragmentCornpetitionHot body=response.body();

                            adapter.addAll(body.getData().getContest_list());

                            binding.setName(body.getData());

                        }

                        @Override
                        public void onFailure(Call<FragmentCornpetitionHot> call, Throwable t) {

                        }
                    });
                    poWinShare.dismiss();
                    break;
                case R.id.popwin_footabl:
                    adapter=new CommentAdapter<>(getActivity(), new ArrayList<FragmentCornpetitionHotContest>(),R.layout.fragment_competition_recycle_item, BR.list );

                    binding.mainListview.setAdapter(adapter);
                    ((TextView) binding.fragmentCompetitionText.getChildAt(0)).setText("中国足球");
                    MyApplication applicationFootbal= (MyApplication) getActivity().getApplication();
                    applicationFootbal.getCompetitionService().getFootball().enqueue(new Callback<FragmentCornpetitionHot>() {
                        @Override
                        public void onResponse(Call<FragmentCornpetitionHot> call, Response<FragmentCornpetitionHot> response) {
                            FragmentCornpetitionHot body=response.body();

                            adapter.addAll(body.getData().getContest_list());

                            binding.setName(body.getData());

                        }

                        @Override
                        public void onFailure(Call<FragmentCornpetitionHot> call, Throwable t) {

                        }
                    });

                    poWinShare.dismiss();
                    break;
                case R.id. popwin_FIFA:
                    adapter=new CommentAdapter<>(getActivity(), new ArrayList<FragmentCornpetitionHotContest>(),R.layout.fragment_competition_recycle_item, BR.list );

                    binding.mainListview.setAdapter(adapter);
                    ((TextView) binding.fragmentCompetitionText.getChildAt(0)).setText("国际足球");
                    MyApplication applicationFIFA= (MyApplication) getActivity().getApplication();
                    applicationFIFA.getCompetitionService().getFIFA().enqueue(new Callback<FragmentCornpetitionHot>() {
                        @Override
                        public void onResponse(Call<FragmentCornpetitionHot> call, Response<FragmentCornpetitionHot> response) {
                            FragmentCornpetitionHot body=response.body();

                            adapter.addAll(body.getData().getContest_list());

                            binding.setName(body.getData());

                        }

                        @Override
                        public void onFailure(Call<FragmentCornpetitionHot> call, Throwable t) {

                        }
                    });

                    poWinShare.dismiss();
                    break;
                case R.id. popwin_basketball:
                    adapter=new CommentAdapter<>(getActivity(), new ArrayList<FragmentCornpetitionHotContest>(),R.layout.fragment_competition_recycle_item, BR.list );

                    binding.mainListview.setAdapter(adapter);

                    ((TextView) binding.fragmentCompetitionText.getChildAt(0)).setText("中国篮球");
                    MyApplication applicationBask= (MyApplication) getActivity().getApplication();
                    applicationBask.getCompetitionService().getBasket().enqueue(new Callback<FragmentCornpetitionHot>() {
                        @Override
                        public void onResponse(Call<FragmentCornpetitionHot> call, Response<FragmentCornpetitionHot> response) {
                            FragmentCornpetitionHot body=response.body();

                            adapter.addAll(body.getData().getContest_list());

                            binding.setName(body.getData());

                        }

                        @Override
                        public void onFailure(Call<FragmentCornpetitionHot> call, Throwable t) {

                        }
                    });
                    poWinShare.dismiss();
                    break;


               // final float centerY = ((ImageView)binding.fragmentCompetitionText.getChildAt(1)).getHeight() / 2.0f;



            }

        }

    }
}
